import {
	Component
} from 'react';
import {
	Tab,
	Button,
	Group,
	Boxs,
	Toast,
	Avatar,
	Dialog
} from 'saltui';
const {
	IconButton
} = Button;
const {
	HBox,
	Box
} = Boxs;
import AngleRight from 'salt-icon/lib/AngleRight';
import PageController from 'pages/home';
import {
	urlPrefix,
	URLS,
	nodataIcon,
	dictMap
} from '../../../app/variables';
import DB from '../../../app/db';
import PostPageItem from 'components/post_page_item';
let tabPage = '0';
export default class PageHome extends Component {

	constructor(props) {
		super(props);
		var token = document.getElementById("token").value;
		var context = document.getElementById("context").value;
		let winHeight = hasBottomTab == 1 ? getScrollHigh(1, 1, 1) : getScrollHigh(1, 1, 0);
		this.state = {
			winHeight: winHeight,
			urlPrev: context + '/api/m/plugin/attachment/mobile/viewImage?fid=',
			token : token,
			integralScore: 0,
			postInfo:{
				replyPostCount: 0,
				postCount: 0,
				collectionCount: 0,
				authorName: '',
				userPhotoPath: '',
				image:'',
				isAdmin:false,
			}
			
		}

		


		dd.biz.navigation.setTitle({
      		title: '我的论坛',
		    onSuccess: function (result) {
		    },
     		 onFail: function () {
      		}
    		});
	}

	//加载待我审核的帖子数量
	componentWillMount() {
		this.initUserInfo();
		this.getIntegralViewInfo();
		this.dislodgeActionSheet();
		Dialog.hide();
	}
	/*componentDidMount(){
		this.handleTabChange(this.state.activeTabIndex);
	}*/

	dislodgeActionSheet(){
      var u = navigator.userAgent;
      var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if(isAndroid==true){
           document.addEventListener('backbutton', function(e) {
            $('.t-popup-mask').trigger('click');
            e.preventDefault();
           });
      }else if(isiOS==true){
          dd.biz.navigation.setLeft({
              control: true,//是否控制点击事件，true 控制，false 不控制， 默认false
              text: '',//控制显示文本，空字符串表示显示默认文本
              onSuccess : function(result) {
                $('.t-popup-mask').trigger('click');
                $('.t-popup-mask').trigger('click');
                backPage();
              },
              onFail : function(err) {}
          });
      }
    }

	initUserInfo() {
		DB.ForumModuleAPI.initUserInfo({
		
		}).then((content) => {
			
			this.setState({
				postInfo: content
			});
		}).catch((error) => {

		});

	}

	getIntegralViewInfo() {
		DB.ForumModuleAPI.getIntegralViewInfo({
		
		}).then((content) => {
			this.setState({
				integralScore: content.integralScore
			});
		}).catch((error) => {

		});

	}
	
	//切换底部tab时，需要将顶部tab切换到最新
	changeTabMy(e){
		tabPage = '0';
	}
	skipPage(field){
		if(field == 'check'){
			location.hash = 'my/check';
		}if(field == 'remind'){
			location.hash = 'my/remindType';
		}else if(field == 'myPost'){
			location.hash = 'my/send';
		}else if(field == 'draft'){
			location.hash = 'my/draft';
		}else if(field == 'collect'){
			location.hash = 'my/collect';
		}
	}
	

	render() {
		let winHeightStyle = {
			height: this.state.winHeight
		};
		var url=this.state.urlPrev;
		var imgUrl = '';
		if(this.state.postInfo.image != null&&this.state.postInfo.image != ''){
			imgUrl = url + this.state.postInfo.image+'&token='+this.state.token;
		}
		return (
		<div>
          <HBox className=" t-PL16 t-PT18 t-PR16 t-PB18 t-BCf t-FAC" vAlign="center">
            <Box flex={1}>
              <Box>
              {
	          	(imgUrl!=null&&imgUrl!='')?<Avatar src={imgUrl}/>: <Avatar name={this.state.postInfo.authorName}  size={40} style={{ fontSize:12,display: 'inline-block'}}/>
	          }
	          </Box>
              <Box className="t-FC3 t-MT10">{this.state.postInfo.authorName}</Box>
            </Box>
            <Box flex={1}>
              <Box className="t-FS16 t-MT4"><b>{this.state.postInfo.postCount}</b></Box>
              <Box className="t-FS14 t-FCddfontblue-60 t-MT10">主题</Box>            
            </Box>
            <Box className="t-BCf7 t-W1 t-H24 t-MT4"></Box> 
            <Box flex={1}>
              <Box className="t-FS16 t-MT4"><b>{this.state.postInfo.replyPostCount}</b></Box>
              <Box className="t-FS14 t-FCddfontblue-60 t-MT10">评论</Box>            
            </Box>
            <Box className="t-BCf7 t-W1 t-H24 t-MT4"></Box> 
            <Box flex={1}>
              <Box className="t-FS16 t-MT4"><b>{this.state.postInfo.collectionCount}</b></Box>
              <Box className="t-FS14 t-FCddfontblue-60 t-MT10">收藏</Box>            
            </Box>          
          </HBox>
					<Box className="t-BCf7 t-H10"></Box>
          <HBox vAlign="center" className={this.state.postInfo.isAdmin == true ? "t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16 t-PR" : "t-DN"}>
            <HBox flex={1} vAlign="center">
              <i className="iconfont icon-headlines t-FCorange t-MR6 t-FS16"></i>
              <span className="t-omit">我的积分</span>
            </HBox>
            <Box>
						<span className="t-omit">{this.state.integralScore}</span>
            </Box>
          </HBox>
          <Box className="t-BCf7 t-H10"></Box>
          <HBox vAlign="center" className="t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16 t-PR">
            <HBox flex={1} vAlign="center" onClick={this.skipPage.bind(this,'remind')}>
              <i className="iconfont icon-headlines t-FCorange t-MR6 t-FS16"></i>
              <span className="t-omit">论坛动态</span>
            </HBox>
            <Box>
              <AngleRight fill="#bbc3cd" width={20} />
            </Box>
          </HBox>
          <Box className="t-BCf7 t-H10"></Box>
					<HBox vAlign="center" className="t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16 dd-bottom-border">
            <HBox flex={1} vAlign="center" onClick={this.skipPage.bind(this,'check')}>
              <i className="iconfont icon-tab-approve t-FCddblue t-MR6 t-FS16"></i>
              <span className="t-omit">审核帖子</span>
            </HBox>
            <Box>
              <AngleRight fill="#bbc3cd" width={20} />
            </Box>
          </HBox>
          <HBox vAlign="center" className="t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16 dd-bottom-border">
            <HBox flex={1} vAlign="center" onClick={this.skipPage.bind(this,'myPost')}>
              <i className="iconfont icon-headlines t-FCddblue t-MR6 t-FS16"></i>
              <span className="t-omit">我的帖子</span>
            </HBox>
            <Box>
              <AngleRight fill="#bbc3cd" width={20} />
            </Box>
          </HBox>
          <HBox vAlign="center" className="t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16 dd-bottom-border">
            <HBox flex={1} vAlign="center" onClick={this.skipPage.bind(this,'draft')}>
              <i className="iconfont icon-report t-FCgreen t-MR6 t-FS16"></i>
              <span className="t-omit">我的草稿</span>
            </HBox>
            <Box>
              <AngleRight fill="#bbc3cd" width={20} />
            </Box>
          </HBox>
          <HBox vAlign="center" className="t-BCf t-PL16 t-PR16 t-PT10 t-PB10  t-FS16">
            <HBox flex={1} vAlign="center" onClick={this.skipPage.bind(this,'collect')}>
              <i className="iconfont icon-collect t-FCyellow t-MR6 t-FS16"></i>
              <span className="t-omit">我的收藏</span>
            </HBox>
            <Box>
              <AngleRight fill="#bbc3cd" width={20} />
            </Box>
          </HBox>
			<PageController changeTabMy={this.changeTabMy.bind(this)} activeIndex={2}/>
			</div>
		);
	}
}